/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web.tags;

import org.apache.commons.lang.StringUtils;
import org.xptracker.util.SideBySideRenderer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.text.MessageFormat;

public class DiffRendererTag extends BodyTagSupport {
  private SideBySideRenderer renderer;
  private String leftHeader;
  private String rightHeader;
  private String caption;

  public SideBySideRenderer getRenderer() {
    return renderer;
  }

  public void setRenderer(SideBySideRenderer renderer) {
    this.renderer = renderer;
  }

  public String getLeftHeader() {
    return leftHeader;
  }

  public void setLeftHeader(String leftHeader) {
    this.leftHeader = leftHeader;
  }

  public String getRightHeader() {
    return rightHeader;
  }

  public void setRightHeader(String rightHeader) {
    this.rightHeader = rightHeader;
  }

  public String getCaption() {
    return caption;
  }

  public void setCaption(String caption) {
    this.caption = caption;
  }

  public int doEndTag() throws JspException {
    try {
      renderer.render(new SideBySideRenderer.Printer<String>() {
        public void printHeader() throws IOException {
          pageContext.getOut().println("<table class=\"diff\">");
          if (caption != null) {
            pageContext.getOut().println("<caption>" + caption + "</caption>");
          }

          if (leftHeader != null || rightHeader != null) {
            pageContext.getOut().println("<thead><tr><th>" + leftHeader +
              "</th><th>" + rightHeader + "</th></tr></thead>");
          }

          pageContext.getOut().println("<tbody>");
        }

        public void printFooter() throws IOException {
          pageContext.getOut().println("</tbody></table>");
        }

        public void printLine(boolean common, String left, String right) throws IOException {
          pageContext.getOut().print("<tr>");

          String leftClass;
          String rightClass;

          if (common) {
            leftClass = rightClass = "common";
          } else {
            leftClass = (left == null ? "leftNotPresent" : "leftPresent");
            rightClass = (right == null ? "rightNotPresent" : "rightPresent");
          }

          pageContext.getOut().print(MessageFormat.format("<td class=\"{0}\">{2}</td><td class=\"{1}\">{3}</td>",
              leftClass, rightClass,
              StringUtils.isBlank(left) ? "&nbsp;" : left,
              StringUtils.isBlank(right) ? "&nbsp;" : right));
          pageContext.getOut().println("</tr>");
        }
      });
    } catch (Exception e) {
      throw new JspException(e);
    }
    return EVAL_PAGE;
  }
}
